package cn.iocoder.yudao.module.cmt.controller.admin.person;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cmt.controller.admin.person.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.person.PersonVolunteerApplyDO;
import cn.iocoder.yudao.module.cmt.service.person.PersonVolunteerApplyService;

@Tag(name = "管理后台 - 志愿者/师资注册申请")
@RestController
@RequestMapping("/cmt/person-volunteer-apply")
@Validated
public class PersonVolunteerApplyController {

    @Resource
    private PersonVolunteerApplyService volunteerApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建志愿者/师资注册申请")
    @PreAuthorize("@ss.hasPermission('cmt:person-volunteer-apply:create')")
    public CommonResult<Long> createVolunteerApply(@Valid @RequestBody PersonVolunteerApplySaveReqVO createReqVO) {
        return success(volunteerApplyService.createVolunteerApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新志愿者/师资注册申请")
    @PreAuthorize("@ss.hasPermission('cmt:person-volunteer-apply:update')")
    public CommonResult<Boolean> updateVolunteerApply(@Valid @RequestBody PersonVolunteerApplySaveReqVO updateReqVO) {
        volunteerApplyService.updateVolunteerApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除志愿者/师资注册申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:person-volunteer-apply:delete')")
    public CommonResult<Boolean> deleteVolunteerApply(@RequestParam("id") Long id) {
        volunteerApplyService.deleteVolunteerApply(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除志愿者/师资注册申请")
    @PreAuthorize("@ss.hasPermission('cmt:person-volunteer-apply:delete')")
    public CommonResult<Boolean> deleteVolunteerApplyList(@RequestParam("ids") List<Long> ids) {
        volunteerApplyService.deleteVolunteerApplyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得志愿者/师资注册申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:person-volunteer-apply:query')")
    public CommonResult<PersonVolunteerApplyRespVO> getVolunteerApply(@RequestParam("id") Long id) {
        PersonVolunteerApplyDO volunteerApply = volunteerApplyService.getVolunteerApply(id);
        return success(BeanUtils.toBean(volunteerApply, PersonVolunteerApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得志愿者/师资注册申请分页")
    @PreAuthorize("@ss.hasPermission('cmt:person-volunteer-apply:query')")
    public CommonResult<PageResult<PersonVolunteerApplyRespVO>> getVolunteerApplyPage(@Valid PersonVolunteerApplyPageReqVO pageReqVO) {
        PageResult<PersonVolunteerApplyDO> pageResult = volunteerApplyService.getVolunteerApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PersonVolunteerApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出志愿者/师资注册申请 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:person-volunteer-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVolunteerApplyExcel(@Valid PersonVolunteerApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PersonVolunteerApplyDO> list = volunteerApplyService.getVolunteerApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "志愿者师资注册申请.xls", "数据", PersonVolunteerApplyRespVO.class,
                        BeanUtils.toBean(list, PersonVolunteerApplyRespVO.class));
    }

}
